package com.lxg.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 陆小根
 * date: 2022/04/21
 * Description:
 * 自动定认证失败后处理的方案
 */

public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
  @Override
  public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    Map<String, Object> result = new HashMap<>();
    result.put("msg", "登录失败:" + exception.getMessage());
    result.put("status", 500);
    response.setContentType("application/json;charset=utf-8");
    String s = new ObjectMapper().writeValueAsString(result);
    response.getWriter().println(s);
  }
}
